有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java If输入与JTable上数据库显示的数据匹配

我只想显示特定日期的数据。如果输入的日期与数据库中的日期相同,我希望特定日期下的其余数据显示在JTable上。我对Java非常陌生,所以请指导我

下面是我的代码

public Welcome() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);
        
        JLabel lblNewLabel = new JLabel("Sales list");
        lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel.setBounds(194, 27, 61, 19);
        contentPane.add(lblNewLabel);
        
        JLabel lblNewLabel_1 = new JLabel("Date");
        lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
        lblNewLabel_1.setBounds(105, 67, 46, 14);
        contentPane.add(lblNewLabel_1);
        
        dateField = new JTextField();
        dateField.setBounds(178, 64, 101, 20);
        contentPane.add(dateField);
        dateField.setColumns(10);
        
        table = new JTable();
        table.setBounds(79, 95, 296, 142);
        contentPane.add(table);
        DefaultTableModel model = new DefaultTableModel(new String[]{"productname", "quantity", "price", "total", "date"}, 0);
        
        JButton btnNewButton = new JButton("Find");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
            
                 try {
                     try {
                        Class.forName("com.mysql.jdbc.Driver");
                    } catch (ClassNotFoundException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    } //loading MySQL drivers. This differs for database servers 
                Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/supermarket", "root", "root");
                
                 PreparedStatement upd = connection.prepareStatement("SELECT * FROM cashier WHERE date =?'");
                 upd.setString(1,dateField.getText());
                 ResultSet rs = upd.executeQuery();
                 
                 while(rs.next())
                 {
                        
                        String productname = rs.getString("productname");
                        String quantity = rs.getString("quantity");
                        String price = rs.getString("price");
                        String total = rs.getString("total");
                        String date = rs.getString("date");
                        model.addRow(new Object[]{productname, quantity, price,total, date });
                        
                 if(dateField.getText().equals(date)) {
                     table.setModel(model);
                     
                 }else {
                     
                 }
                 }
                 } catch (Exception exception) {
                     exception.printStackTrace();
                 }
                 
                 }});
        
        btnNewButton.setBounds(314, 63, 61, 23);
        contentPane.add(btnNewButton);
        
        
            }
        }

我不断地犯这个错误

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''6th Sept''' at line 1

当我在文本字段中输入Sept 6th时会发生这种情况,在该字段中,日期实际上存在于我的数据库中。我不知道这个错误是什么意思

数据库结构

productname    quantity   price   total   date
cookie            1        1        1      6th Sept


共 (0) 个答案